package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import dto.Funcionario;
import dto.Movimentacao;

public class RegistrarMovimentacaoDAO {
	
	private Connection conexao = FabricaConexoesBanco.getInstancia().getConexao();
	
	public void incluir(Movimentacao movimentacao) {
		String sql = "INSERT INTO movimentacao (id_cdd ,pk_matricula, id_objeto, data) values(?,?,?,now())";

		try {
			PreparedStatement pstmt = conexao.prepareStatement(sql);

			pstmt.setInt(1, movimentacao.getIdCdd() );
			pstmt.setInt(2, movimentacao.getFkMatricula());
			pstmt.setInt(3, movimentacao.getIdObjeto());

			pstmt.execute();

			pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public Iterator<Movimentacao> consultarTodas() {
		String sql = "SELECT * FROM movimentacao  order by data desc";
		List<Movimentacao> list = new ArrayList<Movimentacao>();

		try {
			PreparedStatement pstmt = conexao.prepareStatement(sql);

			ResultSet rs = pstmt.executeQuery();

			while (rs.next()) {
				
				Movimentacao mov = new Movimentacao();
				mov.setIdCdd(rs.getInt("id_cdd"));
				mov.setFkMatricula(rs.getInt("pk_matricula"));
				mov.setIdObjeto(rs.getInt("id_objeto"));
				mov.setData(rs.getDate("data").toString());
				
				list.add(mov);

			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return list.iterator();
	}

}
